/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_coupon;


import com.yuecai.modules.yc_brand.domain.YcBrand;
import com.yuecai.modules.yc_brand.repository.YcBrandRepository;
import com.yuecai.modules.yc_category.domain.YcCategory;
import com.yuecai.modules.yc_category.repository.YcCategoryRepository;
import com.yuecai.modules.yc_coupon.domain.YcCoupon;
import com.yuecai.modules.yc_coupon.domain.vo.YcCouponVo;
import com.yuecai.modules.yc_coupon.service.YcCouponService;
import com.yuecai.utils.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* @website https://el-admin.vip
* @author dm
* @date 2020-07-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "yc_coupon管理")
@RequestMapping("/api/ycCoupon")
public class YcCouponController {

    private final YcCouponService ycCouponService;
    private final YcCategoryRepository ycCategoryRepository;
    private final YcBrandRepository ycBrandRepository;


    /**
     * 用户可以领取的 优惠券列表
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<YcCouponVo>> userInitiativeCoupone(){
        String currentUsername = SecurityUtils.getCurrentUsername();
        Long aLong = Long.valueOf(currentUsername);
        List<YcCoupon> ycCouponByType = ycCouponService.findYcCouponByType(3, aLong);
        ArrayList<YcCouponVo> ycCouponVos = new ArrayList<>();
        ycCouponByType.forEach(item->{

            YcCouponVo ycCouponVo = new YcCouponVo();
            BeanUtils.copyProperties(item,ycCouponVo);
            ycCouponVo.setCouponTypeDetails(couponTypeDetals(item.getCouponType(),item.getConditionsId()));
            ycCouponVos.add(ycCouponVo);
        });
        return ResponseEntity.ok(ycCouponVos);
    }


    public String couponTypeDetals(Integer type,Long conditionsId){
//        1:全品类 2:限品类 3:限品牌
        if (type == 1){
            return "全品类";
        }else if (type == 2){
            Optional<YcCategory> byId = ycCategoryRepository.findById(Long.valueOf(conditionsId));
            if (byId.isPresent()){
                String name
                        = byId.get().getName();
                return name;
            }
        }else if (type == 3){
            Optional<YcBrand> byId = ycBrandRepository.findById(Long.valueOf(conditionsId));
            if (byId.isPresent()){
                String brandName
                        = byId.get().getBrandName();
                return brandName;
            }
        }
        return null;
    }



    /**
     * 用户可以领取的 优惠券列表
     * @return
     */
    @GetMapping("/give")
    public ResponseEntity<String> getCouponById(@RequestParam Long id){
        String currentUsername = SecurityUtils.getCurrentUsername();
        Long aLong = Long.valueOf(currentUsername);
        return ycCouponService.getCouponById(id, aLong,3);
    }


}